/**
 * 2011 HuYao
 */
package com.wh.data.pp.bp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.wh.data.model.SumBp;
import com.wh.data.model.SumFp;

/**
 * @author HuYao
 * 
 */
public class SumBpCheckerWSumFp {
    private String fpFilePath = ""; //$NON-NLS-1$
    private String bpFilePath = ""; //$NON-NLS-1$

    final private List<SumBp> sumBpList = new ArrayList<SumBp>();
    final private List<SumFp> sumFpList = new ArrayList<SumFp>();

    /**
     * @param sum_bp_path
     * @param sum_fp_path
     * @throws IOException
     */
    public SumBpCheckerWSumFp(String sum_bp_path, String sum_fp_path)
	    throws IOException {
	this.fpFilePath = sum_fp_path;
	this.bpFilePath = sum_bp_path;

	File bpFile = new File(this.bpFilePath);
	if (!bpFile.exists())
	    throw new FileNotFoundException();
	BufferedReader br = new BufferedReader(new FileReader(bpFile));
	String line = ""; //$NON-NLS-1$
	while ((line = br.readLine()) != null) {
	    StringTokenizer st = new StringTokenizer(line);
	    try {
		int therapist = Integer.parseInt(st.nextToken());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		double total_cost = Double.parseDouble(st.nextToken());
		double total_time = Double.parseDouble(st.nextToken());
		double distance = Double.parseDouble(st.nextToken());
		int day = Integer.parseInt(st.nextToken());
		SumBp tmp = new SumBp(therapist, start, end, total_cost,
			total_time, distance, day);
		this.sumBpList.add(tmp);
		tmp = null;
	    } catch (NumberFormatException e) {
		System.out.println("Skip this line:" + line); //$NON-NLS-1$
	    }
	}
	br.close();
	br = null;

	File fpFile = new File(this.fpFilePath);
	if (!fpFile.exists())
	    throw new FileNotFoundException();
	br = new BufferedReader(new FileReader(fpFile));
	line = ""; //$NON-NLS-1$
	while ((line = br.readLine()) != null) {
	    StringTokenizer st = new StringTokenizer(line);
	    try {
		int therapist = Integer.parseInt(st.nextToken());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		int day = Integer.parseInt(st.nextToken());
		SumFp tmp = new SumFp(therapist, start, end, day);
		this.sumFpList.add(tmp);
		tmp = null;
	    } catch (NumberFormatException e) {
		System.out.println("Skip this line:" + line); //$NON-NLS-1$
	    }
	}
	br.close();
    }

    /**
     * @throws IOException
     */
    public void process() throws IOException {
	for (SumFp sumFp : this.sumFpList) {
	    if (!this.sumBpList.contains(sumFp)) {
		SumBp tmp = new SumBp(sumFp.getTherapist(), sumFp.getStart(),
			sumFp.getEnd(), 0, 0, 0, 0, 0, sumFp.getDay());
		this.sumBpList.add(tmp);
		String dbline = tmp.getTherapist() + " " + tmp.getStart() + " " //$NON-NLS-1$ //$NON-NLS-2$
			+ tmp.getEnd() + " " + tmp.getTotal_cost() + " " //$NON-NLS-1$ //$NON-NLS-2$
			+ tmp.getTotal_time() + " " + tmp.get_distance() + " " //$NON-NLS-1$ //$NON-NLS-2$
			+ tmp.getDay();
		System.out.println(dbline);
		tmp = null;
	    }
	}
	File fpFile = new File(this.bpFilePath);
	BufferedWriter bw = new BufferedWriter(new FileWriter(fpFile));
	for (SumBp sumbp : this.sumBpList) {
	    String line = sumbp.getTherapist() + " " + sumbp.getStart() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getEnd() + " " + sumbp.getTotal_cost() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getTotal_time() + " " + sumbp.get_distance() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getDay();
	    bw.write(line);
	    bw.newLine();
	}
	bw.close();
    }
}
